package com.alibaba.citrus.cr.unified.inventory.functions.invoiceOrder;

import com.alibaba.citrus.cr.unified.inventory.repository.InvoiceOrderWriteRepository;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.epoch.app.crunifiedinventory.dto.request.InvoiceOrderCreateRequest;
import com.epoch.app.crunifiedinventory.sdo.InvoiceOrderDetailSDO;
import com.epoch.app.crunifiedinventory.sdo.InvoiceOrderSDO;
import com.google.common.collect.Lists;
import lombok.Data;

import java.util.List;

@Data
public class InvoiceOrderEntity {

    private InvoiceOrderSDO invoiceOrderSDO;

    private List<InvoiceOrderDetailEntity> invoiceOrderDetailEntityList;

    private static InvoiceOrderWriteRepository invoiceOrderWriteRepository;

    static {
        invoiceOrderWriteRepository = SpringContextUtil.getBean(InvoiceOrderWriteRepository.class);
    }

    public void createInvoiceOrderSDO(InvoiceOrderCreateRequest request) {
        invoiceOrderSDO = convertRequest2InvoiceOrderSDO(request);
    }

    public void add() {
        List<InvoiceOrderDetailSDO> detailSDOS = Lists.newArrayList();
        invoiceOrderDetailEntityList.forEach(invoiceOrderDetailEntity -> {
            detailSDOS.add(invoiceOrderDetailEntity.getInvoiceOrderDetailSDO());
        });
        invoiceOrderSDO.setDetailList(detailSDOS);

        invoiceOrderWriteRepository.create(invoiceOrderSDO);
    }

    private InvoiceOrderSDO convertRequest2InvoiceOrderSDO(InvoiceOrderCreateRequest request) {
        InvoiceOrderSDO invoiceOrderSDO = new InvoiceOrderSDO();
        invoiceOrderSDO.setId(request.getId());
        invoiceOrderSDO.setInvoiceOrderDetailIds(request.getInvoiceOrderDetailIds());
        invoiceOrderSDO.setInvoiceOrderId(request.getInvoiceOrderId());
        invoiceOrderSDO.setReverseInvoiceId(request.getReverseInvoiceId());
        invoiceOrderSDO.setType(request.getType());
        invoiceOrderSDO.setSaleOrganizationCode(request.getSaleOrganizationCode());
        invoiceOrderSDO.setDeliveryOrderId(request.getDeliveryOrderId());
        invoiceOrderSDO.setSourceOrderId(request.getSourceOrderId());
        invoiceOrderSDO.setSapGenerateTime(request.getSapGenerateTime());
        invoiceOrderSDO.setCreateDate(request.getCreateDate());
        invoiceOrderSDO.setCompanyCode(request.getCompanyCode());
        invoiceOrderSDO.setFeature(request.getFeature());
        invoiceOrderSDO.setCreator(request.getCreator());
        invoiceOrderSDO.setSourceOrderCreateTime(request.getSourceOrderCreateTime());

        return invoiceOrderSDO;
    }
    public void update() {
        List<InvoiceOrderDetailSDO> detailSDOS = Lists.newArrayList();
        invoiceOrderDetailEntityList.forEach(invoiceOrderDetailEntity -> {
            detailSDOS.add(invoiceOrderDetailEntity.getInvoiceOrderDetailSDO());
        });
        invoiceOrderSDO.setDetailList(detailSDOS);

        invoiceOrderWriteRepository.update(invoiceOrderSDO);
    }
}
